﻿Namespace FloorItems

    Module mFloorItems

        'TODO: Add in functionality for show/hide item names (+ etc.)

        Public lstItems As SortedDictionary(Of Long, EvoLib.Protocol.FloorItem)

        Public Sub Reset()

            'set the Items list to a new list of 'FloorItem's
            lstItems = New SortedDictionary(Of Long, EvoLib.Protocol.FloorItem)

        End Sub

        Public Sub Add(ByVal item As EvoLib.Protocol.FloorItem)

            If lstItems.ContainsKey(item.INT_ID) Then lstItems.Remove(item.INT_ID)

            'add the item to the array
            lstItems.Add(item.INT_ID, item)


        End Sub

        Public Sub Remove(ByVal id As Long)

            If lstItems.ContainsKey(id) Then

                'remove this entry
                lstItems.Remove(id)

            End If

        End Sub

        Public Sub Draw(ByVal mode As Globals.DrawingMode, ByRef offsetX As Integer, ByRef offsetY As Integer, ByRef originX As Integer, ByRef originY As Integer, ByRef z As Single)


            'Check if this is the Overlay
            'If mode = Globals.DrawingMode.Overlay Then

            'All floor items + text is drawn in the Overlay mode because it is all drawn onto a seperate texture before it is copied to the screen

            'Set the render target to the FloorItems Surface
            'DirectX.D3D.SetRenderTarget(0, DirectX.FloorItemsSurface)

            'Begin the D3D Scene
            'DirectX.D3D.BeginScene()
            'Clear the Target and Z Buffer for the surface
            'DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Microsoft.DirectX.Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'Set the Draw mode to Normal with no tinting etc. (plain)
            'Engine.SetDrawModeNormal(1.0, Color.White)


            'End the D3D Scene
            'DirectX.D3D.EndScene()

            'End If



            'Check if this is the 'Normal' draw mode
            'If mode = Globals.DrawingMode.Normal Then

            'copy the Floor Items Texture to the screen

            'DirectX.BlitD3D(DirectX.FloorItemsTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, tmpz)

            'Draw the Floor Items
            DrawFloorItems(mode, offsetX, offsetY, originX, originY, z)

            'End If

        End Sub

        Private Sub DrawFloorItems(ByRef mode As Globals.DrawingMode, ByRef offsetX As Integer, ByRef offsetY As Integer, ByRef originX As Integer, ByRef originY As Integer, ByRef z As Single)

            If Not (mode = Globals.DrawingMode.Normal Or mode = Globals.DrawingMode.Text) Then Exit Sub

            'TODO: Measure speed of DrawFloorItems subroutine, then optimize drawing if needed

            'Holds number of items in each tile
            Dim ItemCount(GameWindow.mCurrentMap.mWidth, GameWindow.mCurrentMap.mHeight) As Integer
            'holds a temporary X & Y coord
            Dim dx, dy As Integer
            'holds text width & height
            Dim tH As Integer = DirectX.ProFont9.GetNormalTextHeight("X") + 2, tW As Integer
            'holds text color
            Dim c As Color

            'iterate through each entry in the FloorItem list
            For Each item As EvoLib.Protocol.FloorItem In lstItems.Values


                'work out on screen X & Y coords for this tile
                dx = offsetX + ((item.INT2_X - originX) * Constants.TileWidth)
                dy = offsetY + ((item.INT2_Y - originY) * Constants.TileHeight)
                Dim tmpz As Single = (z - ((item.INT2_Y - originY) * 0.005F)) - 0.003F

                If mode = Globals.DrawingMode.Normal Then

                    'Draw the "Ground" Image for the Item
                    With Libs.Ground.Image(item.INT_LOOKS)
                        'DirectX.GameSprite.Begin(Microsoft.DirectX.Direct3D.SpriteFlags.AlphaBlend)
                        .Draw((dx - (.Header.Width / 2)) + (Constants.TileWidth / 2), (dy - (.Header.Height / 2)) + (Constants.TileHeight / 2), tmpz)
                        'DirectX.GameSprite.End()
                    End With

                ElseIf mode = Globals.DrawingMode.Text Then
                    'measure the text width

                    dim strTxt As String = item.STR_NAME

                    If item.INT_COUNT > 1 Then strTxt &= " (" & item.INT_COUNT & ")"

                    tW = DirectX.ProFont9.GetNormalTextWidth(strTxt)

                    'Determine the color of the Item's name text by the Item Class; Loot, Superior... etc.
                    Select Case item.BYTE_ITEMCLASS

                        Case EvoLib.Protocol.ItemClass.Loot
                            c = Color.Gray

                        Case EvoLib.Protocol.ItemClass.Normal
                            c = Color.White

                        Case EvoLib.Protocol.ItemClass.Superior
                            c = Color.LimeGreen

                        Case EvoLib.Protocol.ItemClass.Rare
                            c = Color.Orchid

                    End Select

                    'Draw the Item's name text to the screen
                    DirectX.ProFont9.DrawBorderTextEx(strTxt, (dx + (Constants.TileWidth / 2)) - (tW / 2), dy - (ItemCount(item.INT2_X, item.INT2_Y) * tH), c, Color.Black, Constants.GameTextZ)

                    'add 1 to this square's item count
                    ItemCount(item.INT2_X, item.INT2_Y) += 1

                End If
            Next

            ReDim ItemCount(1, 1)

        End Sub

        Public Sub DoPickup()

            'iterate through all the Items in the list
            For Each item As EvoLib.Protocol.FloorItem In lstItems.Values

                'check for token if it is skip
                If item.INT_ID > -1 Then

                    'check if this Item is in the tile we are standing on at present
                    If item.INT2_X = GameWindow.CoordX And item.INT2_Y = GameWindow.CoordY Then

                        If Player.PlayerWeight.INT4_BAGWEIGHT > Player.Stats.INT2_MAX_WEIGHT Then

                            Dim line As ChatBox.ChatLine
                            line.Msg = "Cannot pick that up! Overweight!"
                            line.Type = EvoLib.Protocol.mProtocol.ChatMsgType.System

                            ChatBox.AddChatLine(line)

                        Else

                            'FloorItemPickup.Enquiry EvoLib.Protocol.Packet Header
                            Dim pkt As EvoLib.Protocol.Packet
                            pkt.PacketType = EvoLib.Protocol.GamePacketType.FloorItemPickup
                            pkt.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry
                            pkt.Username = Globals.sUsername

                            'The payload is just the Item ID that we want to pick up
                            Dim struct As Int64
                            struct = item.INT_ID

                            'Send the Pickup request to the server
                            ClientSock.SendData(EvoLib.Protocol.MakePacket(pkt, struct, Globals.Crypto))

                            'Stop Iterating as we have found the First Item in the current Tile to pickup
                            Exit For

                        End If

                    End If

                End If

            Next

        End Sub

        'Removes any Floor Items that have gone outside of the 'Play Area'
        Public Sub FilterFloorItems()

            'check if lstItems is Nothing
            If lstItems Is Nothing Then Exit Sub

            'if there are no Floor Items in the list, Exit Sub
            If lstItems.Count = 0 Then Exit Sub

            For Each I As EvoLib.Protocol.FloorItem In lstItems.Values

                'check if this Item is outside of the 'Play Area'
                If I.INT2_X < GameWindow.CoordX - Constants.PlayAreaX Or _
                    I.INT2_Y < GameWindow.CoordY - Constants.PlayAreaY Or _
                    I.INT2_X > GameWindow.CoordX + Constants.PlayAreaX Or _
                    I.INT2_Y > GameWindow.CoordY + Constants.PlayAreaY Then

                    'This Item IS outside of the playing area, so...

                    'Remove this Item
                    Remove(I.INT_ID)

                    'call self to remove further Items
                    FilterFloorItems()
                    Exit Sub

                End If

            Next

        End Sub

    End Module

End Namespace